﻿using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MyCompany.WebCrawler.Domain;
using MyCompany.WebCrawler.Repositories;
using MyCompany.WebCrawler.Tasks;
using MyCompany.WebCrawler.Tasks.Tasks;

namespace MyCompany.WebCrawler.Test.Domain.TasksFactoryTests.Given_GetLinksFromTask
{
    [TestClass]
    public class When_ExecuteTheTask : Context
    {
        private string database;
        private const string urlRequest = "http://google.com";
        private const string urlFetched = "http://gooogle.com/ads";
        private GetLinksFromTask task;

        public override void Arrange()
        {
            var page = string.Format("<html><a href=\"{0}\">Ads</a></html>", urlFetched);
            var downloader = new Mock<Downloader>();
            downloader.Setup(d => d.DownloadString(urlRequest)).Returns(page);

            var repository = new Mock<Repository>();
            repository.Setup(r => r.Save(It.IsAny<string>())).Callback<string>(u => database += u);

            var factory = new TasksFactory(null, null, null);
            task = new GetLinksFromTask(factory, downloader.Object, repository.Object);
            task.Url = urlRequest;
        }

        public override void Act()
        {
            task.ExecuteLoad();
        }

        [TestMethod]
        public void Then_LinkWasGottenAndSavedIntoDatabase()
        {
            database.Should().Be(urlFetched);
        }
    }
}